package scau.online.web.user;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.Question;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 网站的一些扩展服务控制器
 * 包括问题反馈 门店信息 之类的
 * Created by Sunlight on 2017/4/5.
 */
@Api(value = "网站扩展服务控制器",description = "问题反馈--")
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController{
    @Autowired
    private IBaseService<Question> questionIBaseService;

    /**
     * 提交问题反馈 要求问题不能超过两百个汉字长度
     * 若用户登录 则保存登录账号
     * 若没有则保存为游客
     * @param question
     * @return
     */
    @ApiOperation(value = "提交问题反馈")
    @ApiImplicitParam(name = "question",value = "问题内容（不超过200个汉字）",required = true,dataType = "String",paramType = "query")
    @PostMapping("/question")
    public String handQuestion(String question){
        Map map = new LinkedHashMap();
        if(question == null || question == "" || question.length()>=200){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
//            logger.info("用户提交问题反馈", Code.ILLEGAL_DATA_ERROR.getStatusMsg());
            return JSON.toJSONString(map);
        }
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        Question question1 = new Question();
        if(username == null){  //判断提交问题反馈用户是否已登录
            question1.setUsername("游客");
        }else {
            question1.setUsername(username);
        }
        question1.setCreateTime(new Date());
        question1.setQuestionContent(question);
        questionIBaseService.add(question1);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

}
